import React, { Component } from 'react'
import RetainedList from './RetainedList'
import style from './Retained.module.scss'
import moment from 'moment';
import { connect } from 'react-redux';
import url from '../../../utils/url';
// import moment from 'moment';
import { removeEmptyField } from '../../compoent/emptyFild';
import {
    Form,
    Button,
    TimePicker,
    DatePicker,
    Select
} from 'antd';
import download from "../../../services/download";
const { RangePicker } = TimePicker;
const { Option } = Select;

class Retained extends Component {
    state = {
        data: [],
        newValues: {}

    }
    onFinish = (values) => {
        const date = values.beginTime
        values.beginTime = moment(date[0]).format('YYYY-MM-DD')
        values.endTime = moment(date[1]).format('YYYY-MM-DD')


        if (values) {
            values.page = 1
            values.size = 10
            const newValues = removeEmptyField(values)
            this.setState({ newValues: newValues })
            this.props.preserve(newValues)
            this.setState({
                data: values
            })
        } else {
            this.props.preserve()
        }
    };
    onFinishFailed = (errorInfo) => {

    };
    exportRetained = () => {
        const { data } = this.state
        download(url.exportPreserve,'GET',{beginTime: data.beginTime,endTime:data.endTime});
     /*   window.location.href = url.exportPreserve + '?beginTime=' + data.beginTime +
            '&endTime=' + data.endTime + '';*/
    }
    render() {
        const { RangePicker } = DatePicker;
        const { newValues } = this.state
        return (
            <div>
                <div className={style.top}>
                    <Form
                        style={{ marginTop: 20, marginLeft: 20 }}
                        name="basic"
                        layout='inline'
                        // wrapperCol={{ span: 6 }}
                        onFinish={this.onFinish}
                        autoComplete="off"
                    >
                        <Form.Item
                            style={{ marginLeft: 860, width: 450 }}
                            label="发起时间"
                            name="beginTime"
                        // rules={[{ message: 'Please input your username!' }]}
                        >
                            <RangePicker
                                // showTime
                                ranges={{
                                    // Today: [moment(), moment()],
                                    '本周': [moment().startOf('week'), moment().endOf('week')],
                                    '本月': [moment().startOf('month'), moment().endOf('month')],
                                }}></RangePicker>
                        </Form.Item>
                        <Form.Item >
                            <Button type="primary" htmlType="submit">
                                查询
                            </Button>
                            <Button type="primary" htmlType="reset" style={{ marginLeft: 50 }}>
                                重置
                            </Button>
                            <Button type="primary" onClick={this.exportRetained} style={{ marginLeft: 50 }}>
                                导出
                            </Button>
                        </Form.Item>

                    </Form>
                </div>
                <div className={style.middle}>
                    <RetainedList newValues={newValues} />
                </div>
            </div>
        )
    }
}

const mapState = state => ({
    ...state.preserve
})
const mapDispatch = dispatch => ({
    ...dispatch.preserve

})

export default connect(mapState, mapDispatch)(Retained);
